package org.example.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.beans.LidarPoint;
import org.example.serve.UdpLidarServer;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/lidar")
@Tag(name = "Lidar 数据接口", description = "提供激光雷达数据的获取、计数、清除以及配置信息的接口")
public class LidarController {

    private final UdpLidarServer udpServer;

    public LidarController(UdpLidarServer udpServer) {
        this.udpServer = udpServer;
    }

    @GetMapping("/points")
    @Operation(summary = "获取所有激光雷达点数据", description = "返回当前存储的所有激光雷达点数据列表",
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功获取激光雷达点数据列表",
                            content = @Content(schema = @Schema(implementation = LidarPoint.class, type = "array")))
            })
    public List<LidarPoint> getAllPoints() {
        return udpServer.getPointCloud();
    }

    @GetMapping("/points/count")
    @Operation(summary = "获取激光雷达点数据的数量", description = "返回当前存储的激光雷达点数据的数量",
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功获取激光雷达点数据数量",
                            content = @Content(schema = @Schema(implementation = Integer.class)))
            })
    public int getPointCount() {
        System.out.println(udpServer.getPointCloud().size());
        return udpServer.getPointCloud().size();
    }

    @DeleteMapping("/points")
    @Operation(summary = "清除所有激光雷达点数据", description = "清除当前存储的所有激光雷达点数据，并返回清除成功的消息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功清除激光雷达点数据",
                            content = @Content(schema = @Schema(implementation = String.class)))
            })
    public String clearPoints() {
        udpServer.clearPoints();
        return "Point cloud cleared";
    }

    @GetMapping("/config/max-point-count")
    @Operation(summary = "获取当前设置的最大激光雷达点数据量", description = "返回当前设置的最大激光雷达点数据量",
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功获取最大激光雷达点数据量",
                            content = @Content(schema = @Schema(implementation = Integer.class)))
            })
    public int getMaxPointCount() {
        return udpServer.getMaxPointCount();
    }
}    